package logic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import data.Qdata;
import database.DatabaseConnection;

import qa.ui.BalanceInformation;
import qa.ui.DepositCash;
import qa.ui.Exit;
import qa.ui.Screen;
import qa.ui.WithdrawlMenu;

public class ATM {
	private Screen screen;
	private Qdata qData;
	private DatabaseConnection db;
	
	public ATM() {

		screen = new Screen();
		qData = new Qdata();
		db = new DatabaseConnection();

		// add listeners to screens
		screen.getWelcome().addActionListener(new WelcomeActionListener());
		screen.getQ1().addActionListener(new Q1ActionListener());
		screen.getQ2().addActionListener(new Q2ActionListener());
		screen.getBalanceInformation().addActionListener(new BalanceInformationActionListener());
		screen.getDepositCash().addActionListener(new BalanceInformationActionListener());
	}

	public void run() {
		screen.show(Screen.WELCOME);
		screen.setVisible(true);
	}

	
	// Listeners

	// Welcome Screen
	private class WelcomeActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			screen.show(Screen.Q1);
		}


	}


	// Q1 screen
	private class Q1ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getQ1().getBtnForward()) {
				String username = screen.getQ1().getUsernameField().getText();
				String password = new String(screen.getQ1().getPasswordField().getPassword());
				boolean test = db.checkUser(Integer.parseInt(username), Integer.parseInt(password));
				if(test)
				{
					qData.setCustomer(screen.getQ1().getCustomer());
					screen.show(Screen.Q2);
				}
			}
			else
				screen.show(Screen.WELCOME);
		}
	}

	// Q2 screen
	private class Q2ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getQ2().getBtnForward()) {
				qData.setChoice(screen.getQ2().getChoice());
				screen.show(Screen.RESULT);
				screen.getResult().show(qData.getCustomer() + "\n" + qData.getChoice());
			}

			if(e.getSource() == screen.getQ2().getbutton1()){
				qData.setBalance(screen.getQ2().getBalance());
				screen.show(Screen.BalanceInformation);
			}else{
				screen.show(Screen.Q1);

				if(e.getSource() ==	screen.getQ2().getbutton2()) {
					qData.setWithdrawl(screen.getQ2().getWithdrawl());
					screen.show(Screen.WithdrawlMenu);
				}else{
					screen.show(Screen.Q1);
					if(e.getSource() == screen.getQ2().getbutton3()) {
						qData.setDeposit(screen.getQ2().getDeposit());
						screen.show(Screen.DepositCash);
					}else{
						screen.show(Screen.Q1);
						if(e.getSource() == screen.getQ2().getbutton4()){
							qData.setExiting(screen.getQ2().getExiting());
							screen.show(Screen.Exit);
						}
						else{
							screen.show(Screen.Q1);
						}
					}
				}
			}
		}
	}
	//			
	////////		//BalanceInformation 	
	private class BalanceInformationActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getBalanceInformation().getbtnBack()) {
				qData.setBalance(screen.getBalanceInformation().getBalance());
				screen.show(Screen.Q2);
			}else
				screen.show(Screen.Q1);
		}

	}
}	